﻿using System;
using System.Collections.Generic;
using System.IO.Pipes;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;

namespace pipeClient
{
    public class Client
    {
        public static void Start()
        {
            Thread serverThread = new Thread(ClientStart);
            serverThread.Start();
            
        }

        public static void ClientStart()
        {
            while (true)
            {
                using (NamedPipeClientStream pipeClient = new NamedPipeClientStream(".", "blockPipe", PipeDirection.InOut))
                {
                    Console.WriteLine("Connecting to server...");
                    pipeClient.Connect();
                    Console.WriteLine("Connected to server.");

                    using (StreamWriter sw = new StreamWriter(pipeClient) { AutoFlush = true })
                    using (StreamReader sr = new StreamReader(pipeClient))
                    {
                        // 发送开始信号
                        sw.WriteLine("StartRun");
                        Console.WriteLine("Sent start signal to server.");

                        // 等待服务器确认
                        string response = sr.ReadLine();
                        Console.WriteLine(response);

                        // 执行逻辑...
                        Console.WriteLine("Executing logic...");

                        // 发送停止信号
                        sw.WriteLine("StopRun");
                        Console.WriteLine("Sent stop signal to server.");

                        // 等待服务器确认
                        response = sr.ReadLine();
                        Console.WriteLine(response);
                    }
                }
            }
        }
    }
}

/*
 * using System;
using System.IO.Pipes;
using System.IO;
using System.Threading.Tasks;

namespace ScratchNet.EMCSharpPLC
{
    public class Client
    {
        public static bool IsDeleteBeforeDll = false;
        public static bool IsDeleteEndDll = false;

        public static void Start()
        {
            _ = ClientStartAsync();
        }

        private static async Task ClientStartAsync()
        {
            
            try
            {
                using (NamedPipeClientStream pipeClient = new NamedPipeClientStream(".", "blockPipe", PipeDirection.InOut))
                {
                    Console.WriteLine("Connecting to server...");
                    await pipeClient.ConnectAsync();
                    Console.WriteLine("Connected to server.");

                    using (StreamWriter sw = new StreamWriter(pipeClient) { AutoFlush = true })
                    using (StreamReader sr = new StreamReader(pipeClient))
                    {
                        while (true)
                        {
                            await CheckAndSendDeleteRequest(sw, sr);
                            // 如果没有请求要发送，则等待一段时间再检查
                            if (!(IsDeleteBeforeDll || IsDeleteEndDll))
                            {
                                await Task.Delay(1000); // 休眠1秒
                            }
                        }
                    }
                }
            }
            catch (IOException e)
            {
                Console.WriteLine($"Error connecting to server: {e.Message}");
                // 等待一段时间后重试连接
                await Task.Delay(5000); // 休眠5秒
            }
            
        }

        private static async Task<bool> CheckAndSendDeleteRequest(StreamWriter sw, StreamReader sr)
        {
            string response = null;

            if (IsDeleteBeforeDll)
            {
                await sw.WriteLineAsync("DeleteBeforeDll");
                response = await sr.ReadLineAsync();
                if (response == "DeleteBeforeSuccess")
                {
                    IsDeleteBeforeDll = false;
                    return true;
                }
            }
            else if (IsDeleteEndDll)
            {
                await sw.WriteLineAsync("DeleteEndDll");
                response = await sr.ReadLineAsync();
                if (response == "DeleteEndSuccess")
                {
                    IsDeleteEndDll = false;
                    return true;
                }
            }

            // 如果没有任何请求需要发送或处理完成，返回 false 表示可以退出循环
            return IsDeleteBeforeDll || IsDeleteEndDll;
        }
        
    }
}

//using System;
//using System.Collections.Generic;
//using System.IO.Pipes;
//using System.IO;
//using System.Linq;
//using System.Text;
//using System.Threading;
//using System.Threading.Tasks;

//namespace ScratchNet.EMCSharpPLC
//{
//    public class Client
//    {
//        public static bool IsDeleteBeforeDll = false;
//        public static bool IsDeleteEndDll = false;

//        public static void Start()
//        {
//            Thread clientThread = new Thread(ClientStart);
//            clientThread.Start();
//        }

//        public static void ClientStart()
//        {
//            using (NamedPipeClientStream pipeClient = new NamedPipeClientStream(".", "blockPipe", PipeDirection.InOut))
//            {
//                Console.WriteLine("Connecting to server...");
//                pipeClient.Connect();
//                Console.WriteLine("Connected to server.");

//                using (StreamWriter sw = new StreamWriter(pipeClient) { AutoFlush = true })
//                using (StreamReader sr = new StreamReader(pipeClient))
//                {
//                    while (true)
//                    {
//                        DeleteDll(sw, sr);
//                    }
//                }
//            }

//        }

//        private static void DeleteDll(StreamWriter sw, StreamReader sr)
//        {
//            string response = string.Empty;
//            if (IsDeleteBeforeDll)
//            {
//                sw.WriteLine("DeleteBeforeDll");
//                // 等待服务端确认删除
//                response = sr.ReadLine();
//            }
//            else if (IsDeleteEndDll)
//            {
//                sw.WriteLine("DeleteEndDll");
//                // 等待服务端确认删除
//                response = sr.ReadLine();
//            }


//            if (response == "DeleteBeforeSuccess")
//            {
//                IsDeleteBeforeDll = false;
//            }
//            else if (response == "DeleteEndSuccess")
//            {
//                IsDeleteEndDll = false;
//            }
//        }
//    }
//}
 */